package com.aiflow.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 
{
    public static String encryptMD5(String source)
    {
        MessageDigest mymd = null;
        try
        {
            mymd = MessageDigest.getInstance("md5");
            //mymd = MessageDigest.getInstance("sha1");
        } catch(NoSuchAlgorithmException exp)
        {
            exp.printStackTrace();
        }

        mymd.update(source.getBytes());
        byte[] result = mymd.digest();
        
        return byte2hex(result);
    }
    
    public static String byte2hex(byte[] source) //������ת�ַ� 
    { 
        String hs = ""; 
        String stmp = ""; 
        for (int n=0; n<source.length; n++) 
        { 
            stmp = (java.lang.Integer.toHexString(source[n] & 0XFF)); 
            if(stmp.length() == 1)
                hs = hs + "0" + stmp; 
            else
                hs = hs + stmp;
        }
        
        //return hs.toUpperCase(); 
        return hs.toLowerCase();
    }     
    
    public static void main(String[] args)
    {


        String s2 = MD5.encryptMD5("123");
        System.out.println(s2);
    }
}
